<?php


   error_reporting(E_ALL & ~ E_NOTICE & E_DEPRECATED & E_USER_DEPRECATED   );
   class Db
   {
       var $_version = '1.0';
       var $_type = 'mysql';
       
       var $_host = 'localhost';
       var $_user = 'root';
       var $_pwd = '';
       var $_db = 'haha';
       
       var $_conn = null;
       
       /**
        * 
        */
       function Db($arrParams = array('host'=>'localhost', 'user'=>'root', 'pwd'=>'131452', 'db'=>'tms'))
       {
           $this->_host = $arrParams['host'];
           $this->_user = $arrParams['user'];
           $this->_pwd = $arrParams['pwd'];
           $this->_db = $arrParams['db'];
           
           $this->_open($this->_db);
       }
       
      
       function query($sql)
       {     
           	$rs = mysql_db_query($this->_db, $sql ) OR $this->_quit(" Query error: <!-- $sql -->");
			return $rs;
       }
	   function mysqlQuery($sql)
       {           
           $rs = mysql_db_query($this->_db, $sql);
           
           return $rs;
       }
       /**
        *
		        */
       function fetchOne($sql)
       {
           $rs = $this->query($sql);
           $arr = mysql_fetch_array($rs, MYSQL_NUM);
           mysql_free_result($rs);
           
           return $arr[0];
       }
       
       /**
        * 
        */
       function fetchRow($sql)
       {
           $rs = $this->query($sql);
           $arr = mysql_fetch_array($rs, MYSQL_ASSOC);
           mysql_free_result($rs);
           
           return $arr;
       }
	  
	   function rowNum($sql){
		   $rs=$this->query($sql);
		   $number=mysql_num_rows($rs);
		   return $number;
	   }
       
      
       function fetchRows($sql)
       {
           $result = $this->query($sql);
           $arr = array();
           while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
           {
               $arr[] = $row;
           }
           mysql_free_result($result);
           
           return $arr;
       }
       
      
       function fetchPage($sql, $page = '1', $pageSize = '20')
       {
           $start = ($page - 1) * $pageSize;
           $arr = $this->fetchRows($sql . " LIMIT $start, $pageSize");
           
           return $arr;
       }
       
       
       function close()
       {
           mysql_close();
       } 
       
      
       function _open($database = 'test')
       {
           $this->_conn = mysql_connect($this->_host, $this->_user, $this->_pwd) || $this->_quit('Connect error: ');
           
           if ($this->_getDbVersion() >= 4.1)
           {
               mysql_query('SET NAMES utf8');
           }
           
           return mysql_select_db($database);
       }
       
      
       function _getDbVersion()
       {
         $rs = mysql_query("SELECT VERSION();");
         $row = mysql_fetch_array($rs, MYSQL_NUM);
         
         $ver = $row[0];
         $vers = explode(".", trim($ver));
         $ver = $vers[0] . ".".$vers[1];
         
         return $ver;
       }
       
       
       function _quit($msg = 'Error: ')
       {
            exit($msg . mysql_error());
           
           return false;
       }
   }
   
?>